<?php
/**
 * 新闻动态
 * Created by PhpStorm.
 * User: xsh
 * Date: 2018/4/12  10:06
 */
namespace app\pc\Controller;
use think\Controller;

class Center extends Base
{
    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
        $this->assign('action',request()->action());
        $data = db('member')->where('id', session('info_user')['id'])->find();
        $num = unserialize($data['num']);
        $name = db('member_authority')->where(array('id'=>session('info_user')['type']))->field('name')->find();

        //判断该会员是否过期
        if(time() > $this->info_user['validity_time'] && $this->info_user['validity_time'] != '0'){
            $member_validity_time = true;
        }else{
            $member_validity_time = false;
        }
        $this->assign('member_validity_time',$member_validity_time);
        $this->assign('name', $name);
        $this->assign('num', $num);
        $this->assign('data', $data);

    }

    //我的关注
    public function index(){
        $uid = $this->info_user['id'];

        $attention_result = db('attention')->alias('a')->join('xls_company xc','xc.id = a.company_id and a.status=1')->where(array('a.uid'=>$uid))->field('a.id as aid,a.status,xc.*')->order('a.id desc')->paginate(10)->each(function($item,$key){
            $are_id = [$item['province'],$item['city'],$item['area']];
            $where['id'] = array('in',$are_id);
            $area_result = db('area')->where($where)->field('name')->order('id asc')->select();
            $item['province'] = isset($area_result[0]['name'])?$area_result[0]['name']:'';
            $item['city'] = isset($area_result[1]['name'])?$area_result[1]['name']:'';
            $item['area'] = isset($area_result[2]['name'])?$area_result[2]['name']:'';
            $item['path_tem'] = $item['province'].$item['city'].$item['area'];

            if($item['status'] == '1'){
                //已关注
                $item['is_attention'] = $item['status'];
            }else{
                //未关注
                $item['is_attention'] = '-1';
            }

            return $item;
        });
        $page = $attention_result->render();
          //var_dump($attention_result);exit();
        $this->assign('attention_result',$attention_result);

        $this->assign('page',$page);
        $this->assign('page',$page);
        return $this->fetch();
    }

    //下载记录
    public function down_recording(){
        $uid = $this->info_user['id'];

        $down_record = db('down_record')->where(array('uid'=>$uid))->order('time desc')->paginate(10)->each(function($item,$key){
                switch ($item['type']) {
                    case '1':
                        $result = db('publicity')->where(array('id'=>$item['pid']))->field('title')->find();
                        //$url=url('publicity/detail',array('id'=>$item['pid']));
                        $type_tile = '公示公告';
                        break;
                    case '2':
                        $result = db('notice')->where(array('id'=>$item['pid']))->field('title')->find();
                        //$url=url('notice/detail',array('id'=>$item['pid']));
                        $type_tile = '通知公告';
                        break;
                    case '3':
                        $result = db('policy')->where(array('id'=>$item['pid']))->field('title')->find();
                        //$url=url('policy/detail',array('id'=>$item['pid']));
                        $type_tile = '政策文件';
                        break;
                }
                $item['title'] = isset($result['title'])?$result['title']:'';
                $item['url'] = isset($url)?$url:'';
                $item['type_tile'] = isset($type_tile)?$type_tile:'';

                $file_result = db('file')->where(array('id'=>$item['fid']))->field('file_name')->find();

                $item['file_name'] = $file_result['file_name'];
                $item['time'] = date('Y-m-d',$item['time']);

                return $item;
        });
        $page = $down_record->render();
        
        $this->assign('down_record', $down_record);
        $this->assign('page', $page);
        return $this->fetch();
    }

    //账号资料
    public function member_data(){

        return $this->fetch();
    }

    //联系我们
    public function contact_us(){
        $web_set_result = db('web_set')->field('telephone,qr')->find();
        $this->assign('domain', $_SERVER['HTTP_HOST']);
        $this->assign('contact_us', $web_set_result);
        return $this->fetch();
    }

    public function headimage_edit(){
        if(request()->isAjax()){
            $headimage = request()->file('headimage');
            if(!empty($headimage)){
                $info = $headimage->move(ROOT_PATH . 'public/static/uploads');
                if ($info) {
                    $data['headimage'] = $info->getSaveName();
                }
            }
            $bool = db('member')->where(array('id'=>session('info_user')['id']))->update($data);

            if(!$bool)$this->error('修改失败');
            $this->success('修改成功!');
        }
    }

    public function edit(){
        if(request()->isAjax()){
            $data['username'] = input('username');
            $data['mobile'] = input('mobile');
            $data['company'] = input('company');
            $passwd =input('passwd');
            $data['sex'] = input('sex');
            $member = db('member')->where('id', session('info_user')['id'])->find();
            if($passwd != $member['passwd']){
                $data['passwd'] = md5($passwd);
            }else{
                $data['passwd'] = $passwd;
            }
            $bool = db('member')->where('id', session('info_user')['id'])->update($data);

            if($bool){
                $this->success('修改成功!',url('center/member_data'));
            }else{
                $this->error('修改失败',url('center/member_data'));
            }
        }
    }

    public function ajax_right(){
        $id = input('id');
        $user_find = db('member')->where('id', $id)->field('num,username')->find();
        $num = unserialize($user_find['num']);
        //halt($num);
       //dump($num);exit();
        $this->assign('num', $num);
        $this->assign('user_find',$user_find);
        return $this->fetch();
    }

    //套餐购买
    public function buy_service(){
        $user_id=$_GET['uid'];
     
        $list = db('member_authority')->where(array('status'=>'1'))->order('id','desc')->paginate(10)->each(function($item,$key){
             $item['formula'] = unserialize($item['formula']);
             $item['circular'] = unserialize($item['circular']);
             $item['policy'] = unserialize($item['policy']);
             return $item;
        });

        $page = $list->render();

        $this->assign('list',$list);
        $this->assign('user_id',$user_id);
        $this->assign('page',$page);
        return $this->fetch();
       
    }

    public function ajax_info(){
        $id = input('id');

        $num = db('member_authority')->where(array('status'=>'1','id'=>$id))->find();
     
        $num['formula'] = unserialize($num['formula']);
        $num['circular'] = unserialize($num['circular']);
        $num['policy'] = unserialize($num['policy']);
        
        $this->assign('num', $num);
        return $this->fetch();
    }
}